<?php
namespace app\index\controller;

use app\index\model\Jianlis;
use app\index\model\Jianlisqz;
use think\Db;
use app\index\model\Job;
use app\common\controller\IndexBase;


class Zhiwei extends IndexBase
{
    public function lists()
    {   
        $city_list = db('job')->column('city');
        //去重
        $city_list = array_unique($city_list);
        $name_list = db('job')->column('jobname');
        //去重
        $name_list = array_unique($name_list);
        //职位名称模糊搜索
        if (input('post.')) {
            $name = input('post.name');
            if ($name) {
                $where['jobname']  = ['like','%'.$name.'%'];
            }
        }
        //城市搜索
        if ($_GET['city'] && $_GET['city'] != 1) {
            $where['city'] = input('get.city');
        }
        //职位名称搜索
        if ($_GET['jobname'] && $_GET['jobname'] != 1) {
            $where['jobname'] = input('get.jobname');
        }
        $where['is_show']  = 1;
        $res = model('job')->where($where)->field('jobid,cid,logo,jobname,wage,city,is_index,update_time,reading')->order('is_index desc,sort desc,update_time desc,add_time desc,jobid desc')->select();
        return $this->fetch('list',['list'=>$res,'city_list'=>$city_list,'name_list'=>$name_list]);
    }
    public function index()
    {   
        $id = input('get.id');
        $res = model('job')->where(['jobid'=>$id,'is_show'=>1])->find();//选中职位信息
        //当职位存在以及可投递时
        if ($res) {
            //推荐3个简历
            $res_index = model('job')->where(['is_index'=>1,'is_show'=>1])->field('jobid,logo,cid,jobname,wage,city,is_index,update_time,reading')->order('is_index desc,sort desc,update_time desc,jobid desc')->limit(3)->select();
            //职位所属公司ID
            $res['getcid'] = model('job')->where(['jobid'=>$id,'is_show'=>1])->value('cid');
            //阅读量加1
            model('job')->where('jobid', $id)->setInc('reading');
            //当前用户是否关注当前职位
//            $gz_zw_list = model('users')->where('openid',session('wx'))->value('zw');
//            $gz_zw_arr = explode(',', $gz_zw_list);
//            if (in_array($id, $gz_zw_arr)) {
//                $zw_gz = 1;
//            }else{
//                $zw_gz = 0;
//            }
            //是否有简历创建
            //兼职
            $jl = model('jianlis')->where('openid',session('wx'))->count();
            //全职
            $jlqz = model('jianlisqz')->where('openid',session('wx'))->count();
            //是否投递过当前职位
            $toudi_lsit = model('toudi')->where(['openid'=>session('wx'),'jobid'=>$id])->count();
            if ($toudi_lsit) {
                $toudi_state= 1;  
            }else{
                $toudi_state= 0;  
            }
            //发送投递状态
            $this->assign('toudi_state',$toudi_state);
			if (!$jl && !$jlqz) {
                $this->assign('wzd','未创建');
            }else{
                $this->assign('jl',$jl);
                $this->assign('jlqz',$jlqz);
            }
			//关注状态
//            $this->assign('zw_gz',$zw_gz);
			//职位信息
            $this->assign('job_data',$res);
            //推荐职位
            $this->assign('job_index',$res_index);
            return $this->fetch('index');
        }else{
            echo "<script language='javascript' type='text/javascript'> alert('该职位已下线');location.href='javascript:history.back(-1)';</script>"; 
        }
    }
//    public function zw_gz()
//    {
//
//        $id = input('post.id');
//
//        $openid = session('wx');
//        $gz_zw_list = db('users')->where('openid',$openid)->value('zw');
//        $gz_zw_arr = explode(',', $gz_zw_list);
//        if (in_array($id, $gz_zw_arr)) {
//            foreach ($gz_zw_arr as $key=>$value){
//                if ($value === $id)
//                    unset($gz_zw_arr[$key]);
//            }
//            $gz_type= 1;
//        }else{
//            array_push($gz_zw_arr,$id);
//            $gz_type= 0;
//        }
//
//        $data['zw'] = implode(',', $gz_zw_arr);
//        $res = db('users')->where('openid',$openid)->update($data);
//        if ($res) {
//            if ($gz_type) {
//                return json(['code'=>1,'msg'=>'取消关注']);
//            }else{
//                return json(['code'=>2,'msg'=>'关注成功']);
//            }
//
//        }else{
//            return json(['code'=>0,'msg'=>'操作错误']);
//        }
//    }
    public function toudiAdd()
    {
        //未投递新增投递记录
        $data['openid'] = session('wx');
        $data['jobid'] = input('post.jobid');//职位ID
        $data['jlid'] = input('post.id');//简历ID
        $data['type'] = input('post.type');//类型
        $data['add_time'] = date("Y-m-d H:i:s",time());
        model('toudi')->insert($data);
        return json(['code'=>1,'msg'=>'投递成功']);
    }
    public function toudi()
    {
        $jobid = input('post.id');//职位ID
        $openid = session('wx');
        //当前职位是否已投递
        $count = model('toudi')->where(['openid'=>$openid,'jobid'=>$jobid])->count();
        if ($count) {
            //已投递则删除投递记录
            model('toudi')->where(['openid'=>$openid,'jobid'=>$jobid])->delete();
            return json(['code'=>1,'msg'=>'取消投递成功']);
        }else{
            return json(['code'=>0]);
        }
    }
    public function gz_list()
    {
        $openid = session('wx');
        $gz_zw_list = db('users')->where('openid',$openid)->value('zw');
        $gz_zw_arr = explode(',', $gz_zw_list);

        $res = model('job')->where('jobid','in',$gz_zw_arr)->field('jobid,cid,jobname,wage,city,is_index,update_time,reading,is_show')->order('is_index desc,sort desc,update_time desc,add_time desc,jobid desc')->select();
        // var_dump($res);exit();
        if ($res) {
            return $this->fetch('gz_list',['list'=>$res]);
        }else{
            return $this->fetch('gz_list',['list'=>'']);
        }


    }
    public function jlist()
    {
        $this->assign('jobid',input('id'));
        $jianlis = new Jianlis();
        $jianlisqz = new Jianlisqz();
        $list_qz = $jianlisqz->where(['openid'=>session('wx')])->field('id,jname,add_time,update_time')->order('update_time desc,add_time desc')->select();
        $list = $jianlis->where(['openid'=>session('wx')])->field('id,jname,add_time,update_time')->order('update_time desc,add_time desc')->select();
        return $this->fetch('jlist',['list'=>$list,'list_qz'=>$list_qz]);
    }
}